cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-03 LANGUAGES CXX)

# 设置C++语言的特性
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找Python的解释器
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT REQUIRED)

# numpy.__version__:  /usr/lib/python3/dist-packages/numpy/__init__.py'
execute_process(COMMAND ${PYTHON_EXECUTABLE} "-c" "import re, numpy; print(re.compile('/__init__.py.*').sub('', numpy.__file__))"
        RESULT_VARIABLE _numpy_status                                       # 0
        OUTPUT_VARIABLE _numpy_location                                     # /usr/lib/python3/dist-packages/numpy
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE)
if (NOT _numpy_status)
    set(NumPy ${_numpy_location} CACHE STRING "location of Numpy")          # 将_numpy_location存储在新变量NumPy中
endif ()
#include(CMakePrintHelpers)                                                 # 调试输出
#cmake_print_variables(_numpy_status _numpy_location)

execute_process(COMMAND ${PYTHON_EXECUTABLE} "-c" "import numpy; print(numpy.__version__)"
        OUTPUT_VARIABLE _numpy_version                                      # 1.13.3
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE)

include(FindPackageHandleStandardArgs)
# Found Numpy: /usr/lib/python3/dist-packages/numpy (found version "1.13.3")
find_package_handle_standard_args(Numpy
        FOUND_VAR Numpy_FOUND
        REQUIRED_VARS NumPy
        VERSION_VAR _numpy_version)

add_executable(pure-embedding "")
target_sources(pure-embedding PRIVATE Py${PYTHON_VERSION_MAJOR}-pure-embedding.cpp)
target_include_directories(pure-embedding PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(pure-embedding PRIVATE ${PYTHON_LIBRARIES})

# 将 use_numpy.py 复制到 build 目录
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/use_numpy.py
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/use_numpy.py ${CMAKE_CURRENT_BINARY_DIR}/use_numpy.py
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/use_numpy.py)

# 添加源文件use_numpy.py
target_sources(pure-embedding PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/use_numpy.py)
